package phoenics.manpads.stinger.controller;

import de.felixroske.jfxsupport.FXMLController;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import org.controlsfx.validation.Severity;
import org.controlsfx.validation.ValidationSupport;
import org.controlsfx.validation.Validator;
import org.springframework.beans.factory.annotation.Autowired;
import phoenics.manpads.common.utils.ValidationUtil;
import phoenics.manpads.stinger.dao.ProjectDao;
import phoenics.manpads.stinger.domain.po.Project;
import phoenics.manpads.stinger.model.ProjectModel;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * 编辑项目信息
 *
 * @author Phoenics Chow
 * on 2019年01月20日
 */
@FXMLController
public class EditProjectController implements Initializable {
    private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(EditProjectController.class);
    ValidationSupport validationSupport = new ValidationSupport();
    public static final ProjectModel projectModel=new ProjectModel();
    @FXML TextField projectName ;
    @FXML TextField projectCnName ;
    @FXML
    ComboBox dbType ;
    @FXML TextField dfPackageHead ;
    @FXML TextField modelName ;
    @FXML TextField author ;
    @FXML TextField email ;
    @FXML TextField tablePrefix ;
    @FXML TextField projectPath ;
    @FXML
    TextArea projectComment;
    @Autowired
    ProjectDao projectDao;
    @Autowired
    MainController mainController;
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        registerValidator();
        dbType.getItems().addAll("q1","q2");
        //projectName.textProperty().bindBidirectional(projectModel.snameProperty());


    }
    public void closeWindow(ActionEvent event){
        ((Stage)((Button)event.getTarget()).getScene().getWindow()).close();
    }
    public void showInfo(ActionEvent event){
        Project project=projectModel.takeObjectWarpper(new Project());
        if(project!=null){
            projectDao.saveProject(project);
            mainController.reloadProjectTree();
        }
    }
    private void registerValidator(){
        validationSupport.registerValidator(projectName, Validator.createRegexValidator("项目代码不能为空，并且不能重复",ValidationUtil.STR_ENG_NUM_,Severity.ERROR));
        validationSupport.registerValidator(projectCnName, Validator.createEmptyValidator("项目中文名不能为空"));
        //validationSupport.registerValidator(dbType, Validator.createEmptyValidator("项目代码不能为空，并且不能重复"));
        validationSupport.registerValidator(dfPackageHead, Validator.createRegexValidator("java包标示不对",ValidationUtil.JAVAPACKAGE,Severity.ERROR));
        validationSupport.registerValidator(modelName, Validator.createRegexValidator("模块名称",ValidationUtil.JAVAPACKAGE,Severity.ERROR));
        //validationSupport.registerValidator(author, Validator.createEmptyValidator("项目代码不能为空，并且不能重复"));
        validationSupport.registerValidator(email, Validator.createRegexValidator("email格式错误", ValidationUtil.EMAIL, Severity.ERROR));
    }

}
